package com.googlecode.spruce.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.spruce.dao.AbstractDao;
import com.googlecode.spruce.dao.ServiceResultDao;
import com.googlecode.spruce.domain.Host;
import com.googlecode.spruce.domain.ServiceResult;

@Repository
@Transactional
public class DefaultServiceResultDao extends AbstractDao<Integer, ServiceResult> implements ServiceResultDao
{

    public DefaultServiceResultDao()
    {
        super(ServiceResult.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ServiceResult> getPageByHostID(final Integer hostID, final int pageOffset, final int pageSize)
    {
        final Query query = super.getSession().createQuery(
                "from ServiceResult where host.id = :hostID order by cdate desc");

        query.setParameter("hostID", hostID);
        query.setFirstResult(pageOffset);
        query.setMaxResults(pageSize);

        return query.list();
    }

    @Override
    public ServiceResult getLastError(final Host host)
    {
        final Query query = super.getSession().createQuery("from ServiceResult where host = :host and result=0");

        query.setParameter("host", host);
        query.setMaxResults(1);

        return (ServiceResult) query.uniqueResult();
    }
}
